package com.msk.wlhy.service.thread;


import com.msk.common.utils.StringUtils;
import com.msk.sys.utils.DocUtils;
import com.msk.sys.utils.PdfUtil;
import com.msk.third.service.OssServerService;
import com.msk.wlhy.domain.HtTransportList;
import com.msk.wlhy.mapper.HtTransportListDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Map;

/**
 * 基础资料证据链的生成
 * @author cmy
 */
@Service
@Transactional(readOnly = true)
public class TransportContractService {

    @Autowired
    private HtTransportListDao htTransportListDao;
    
    @Autowired
    private OssServerService ossServerService;

    final String ftlName = "carriageContract";

    @Transactional(readOnly = false)
    public int genFile(String id, String titl, Map<String, Object> map) {

        DocUtils docUtil = new DocUtils();

        String sep = File.separator;

        String s1 = System.getProperty("user.dir");

        int idx = s1.indexOf(":");

        String s2 = idx>0 ? s1.substring(0, idx+1) : "";

        String dirPath = s2+sep+"ftlOut"+sep;

        File f = new File(dirPath);
        if (!f.exists()) {
            f.mkdirs();
        }

        String filePath = dirPath +  titl + "承运合同.doc";
        File checkFile = new File(filePath);
        if (checkFile.exists()) {
            checkFile.delete();
        }
        //填充模板数据,并生成doc文件
        boolean flag = docUtil.createDocNew(map, ftlName, filePath);
        if(flag) {
            try {
                filePath = ossServerService.uploadFile2Oss(new File(filePath),titl + ".doc");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                this.genPdf(id, filePath, titl);

                return 1;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }else{
            return 0;
        }
    }

    /** 异步
     * @by cmy
     * @param
     * @return
     */
    public int genPdf(String id, String docPath, String titl) {
        try{
            //第一步 word转PDF
            // ossPrefix
            String pdfPath = PdfUtil.wordToPdf(docPath);
            try {
                pdfPath = ossServerService.uploadFile2Oss(new File(pdfPath),titl + ".pdf");
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (StringUtils.isBlank(pdfPath)) {
                return 0;
            }
            //第二步
            File unsignFile = new File(pdfPath);
            if (unsignFile == null) {
                return 0;
            }

            HtTransportList htTransportList = new HtTransportList();
            htTransportList.setId(id);
            htTransportList.setPlatDriverDocFile(docPath);
            htTransportList.setPlatDriverPdfFile(pdfPath);
            htTransportList.preUpdate();
            htTransportListDao.updateFile(htTransportList);

            return 1;
        }catch(Exception e){
            e.printStackTrace();
            return 0;
        }

    }

}
